package com.cyys.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyys.entity.Category;
import com.cyys.entity.CategoryGoods;
import com.cyys.entity.Sites;
import com.cyys.response.Result;
import com.cyys.service.SitesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Long
 * @since 2022-06-21
 */
@RestController
@RequestMapping("/sites")
public class SitesController {

    @Autowired
    private SitesService sitesService;

    @GetMapping("/addSites")
    public Result addsitesCart(@RequestParam(name = "user_id", value = "user_id") Integer user_id,
                               @RequestParam(name = "sex", value = "sex") Integer sex,
                               @RequestParam(name = "iphone", value = "iphone") String iphone,
                               @RequestParam(name = "name", value = "name") String name,
                               @RequestParam(name = "profile", value = "profile") String profile,
                               @RequestParam(name = "site", value = "site") String site,
                               @RequestParam(name = "label", value = "label") String label,
                               @RequestParam(name = "defaults", value = "defaults", required = false) String defaults) {

        System.out.println(defaults);
        if (defaults.equals("true")) {
            /**
             * 修改
             */
            sitesService.updateDefaults(user_id);
            System.out.println("设置默认地址");
        }

        Sites s = new Sites();
        s.setUser_id(user_id);
        s.setSex(sex);
        s.setIphone(iphone);
        s.setName(name);
        s.setProfile(profile);
        s.setSite(site);
        s.setLabel(label);
        s.setDefaults(defaults);
        sitesService.save(s);
        return Result.ok().message("录入信息成功");
    }

    /**
     * @param user_id
     * @return
     */
    @GetMapping("/Sites_id")
    public Result Sites_id(Integer user_id) {

        Sites ca = new Sites();
        ca.setUser_id(user_id);
        List<Sites> list = sitesService.Sites_id(ca);

        return Result.ok().data("list", list);
    }
    /**
     * @param id
     * @return
     */
    @GetMapping("/id")
    public Result id(Integer id) {
        Sites list = sitesService.getById(id);

        return Result.ok().data("list", list);
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("/Delete")
    public Result delete(Integer id) {
        boolean list = sitesService.removeById(id);

        if (list) {
            return Result.ok().message("成功删除");
        }
        return Result.error().message("删除失败");
    }


    @GetMapping("/updatesites")
    public Result updatesitesCart(@RequestParam(name = "id", value = "id") Integer id,
                                  @RequestParam(name = "user_id", value = "user_id", required = false) Integer user_id,
                               @RequestParam(name = "sex", value = "sex", required = false) Integer sex,
                               @RequestParam(name = "iphone", value = "iphone", required = false) String iphone,
                               @RequestParam(name = "name", value = "name", required = false) String name,
                               @RequestParam(name = "profile", value = "profile", required = false) String profile,
                               @RequestParam(name = "site", value = "site", required = false) String site,
                               @RequestParam(name = "label", value = "label", required = false) String label,
                               @RequestParam(name = "defaults", value = "defaults", required = false) String defaults) {
        if (defaults.equals("true")) {
            /**
             * 修改
             */
            sitesService.updateDefaults(user_id);
            System.out.println("设置默认地址");
        }
        Sites s = new Sites();
        s.setId(id);
        s.setUser_id(user_id);
        s.setSex(sex);
        s.setIphone(iphone);
        s.setName(name);
        s.setProfile(profile);
        s.setSite(site);
        s.setLabel(label);
        s.setDefaults(defaults);
        UpdateWrapper up=new UpdateWrapper();
        up.eq("id",id);
        boolean update = sitesService.update(s, up);
        if (update) {
            return Result.ok().message("录入信息成功");
        }
        return Result.error().message("录入信息成失败");

    }

    /**
     * @param
     * @return
     */
    @GetMapping("/sitesuid")
    public Result sitesUid(Integer uid,Integer id) {
        Sites ca = new Sites();
        ca.setUid(uid);
        if (id == null||id.equals("null")) {
            List<Sites> list = sitesService.sites_defaults(uid);
            if (list.size()==0) {
                return Result.error().message("该用户没有数据");
            }else {
                List<Sites> list1 = sitesService.sites_Limit(uid);
                return Result.ok().data("list", list1);
            }
        }
        ca.setId(id);
        List<Sites> list = sitesService.sitesuid(ca);
        if (list.size()==0) {
            return Result.error().message("查找不到该数据");
        }

        return Result.ok().data("list", list);
    }


}

